package xu.ye.view;

import com.agent.crm.db.DBInitScript;
import com.agent.crm.db.DatabaseManager;
import com.agent.crm.view.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddEditTypeActivity extends Activity  {
    private EditText et_typeId;
    private EditText et_typeName;
    private Button btn_save;
    private Button btn_exit;
    private DatabaseManager manager = null;
    private Intent intent;
    private Boolean bool;
    private int position = -1;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        manager = new DatabaseManager(this);
        
        setContentView(R.layout.add_edit_type);
        et_typeId = (EditText)findViewById(R.id.editTypeId);
        et_typeName = (EditText)findViewById(R.id.editTypeNameId);
        btn_save = (Button)findViewById(R.id.editTypeSaveId);
        btn_exit = (Button)findViewById(R.id.editTypeExitId);
        btn_save.setOnClickListener(clickListener);
        btn_exit.setOnClickListener(clickListener);
        intent = getIntent();
        bool = intent.getBooleanExtra("edit", false);
        if(bool){
            et_typeId.setText(intent.getIntExtra("type_id", -1) + "");
            et_typeName.setText(intent.getStringExtra("type_name"));
            position = intent.getIntExtra("position", -1);
        }
    }
    
    OnClickListener clickListener = new OnClickListener(){
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.editTypeSaveId:
                
                String typeName = et_typeName.getText().toString();
                if(typeName.trim().equals("")){
                    Toast.makeText(AddEditTypeActivity.this, "组名不许为空", Toast.LENGTH_LONG).show();
                    return;
                }
                SQLiteDatabase SQLite = manager.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("type_name", typeName);
                Bundle bundle = new Bundle();
                bundle.putString("type_name", typeName);
                
                if(bool){
                    bundle.putString("type_id", et_typeId.getText().toString());
                    int result = SQLite.update(DBInitScript.Table_TypeInfo, values, "type_id=?", new String[]{et_typeId.getText().toString()});
                    if(result == -1 ) {
                        Toast.makeText(AddEditTypeActivity.this, "更新失败", Toast.LENGTH_LONG);
                    }
                    setTitle("分类名称修改成功");
                    bundle.putInt("position", position);
                }
                else{
                    values.put("sys_config", 0);
                    long result = SQLite.insert(DBInitScript.Table_TypeInfo, null, values);
                    bundle.putInt("type_id", (int)result);
                    //通过结果来判断是否插入成功，若为1，则表示插入数据失败
                    if(result == -1 ) {
                        Toast.makeText(AddEditTypeActivity.this, "添加失败", Toast.LENGTH_LONG);
                    }
                    setTitle("添加成功");
                }
                SQLite.close();
                Intent resultIntent = new Intent();
                resultIntent.putExtras(bundle);
                
                if(bool){
                  //返回到上一个Activity
                    setResult(3, resultIntent); 
                }else {
                  //返回到上一个Activity
                    setResult(4, resultIntent); 
                }
                finish();
                break;
                
            case R.id.editTypeExitId:
                finish();
                break;
                
            default:
                break;
            }
            
            
        }
    };
    
}
